{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyproj\n",
    "import numpy as np\n",
    "import geoviews as gv\n",
    "import geoviews.feature as gf\n",
    "from bokeh.sampledata.airport_routes import airports, routes\n",
    "\n",
    "gv.extension('matplotlib')\n",
    "\n",
    "gv.output(fig='svg', size=250)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_circle_path(start, end, samples=200):\n",
    "    sx, sy = start\n",
    "    ex, ey = end\n",
    "    g = pyproj.Geod(ellps='WGS84')\n",
    "    (az12, az21, dist) = g.inv(sx, sy, ex, ey)\n",
    "    lonlats = g.npts(sx, sy, ex, ey, samples)\n",
    "    return np.array([(sx, sy)]+lonlats+[(ex, ey)])\n",
    "\n",
    "# Compute great-circle paths for all US flight routes from Honolulu\n",
    "paths = []\n",
    "honolulu = (-157.9219970703125, 21.318700790405273)\n",
    "routes = routes[routes.SourceID==3728]\n",
    "airports = airports[airports.AirportID.isin(list(routes.DestinationID)+[3728])]\n",
    "for i, route in routes.iterrows():\n",
    "    airport = airports[airports.AirportID==route.DestinationID].iloc[0]\n",
    "    paths.append(get_circle_path(honolulu, (airport.Longitude, airport.Latitude)))\n",
    "\n",
    "# Define Graph from Nodes and EdgePaths\n",
    "path = gv.EdgePaths(paths)\n",
    "points = gv.Nodes(airports, ['Longitude', 'Latitude', 'AirportID'], ['Name', 'City'])\n",
    "graph = gv.Graph((routes, points, path), ['SourceID', 'DestinationID'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gf.ocean * gf.land * gf.lakes * gf.coastline * graph.opts(\n",
    "    node_color='black', node_size=8)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
